package dth.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import dth.entity.Package;
import dth.entity.User;
import dth.entity.UserPackage;

public class UserPackageDAO {
	private SessionFactory sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
	private Session session=sessionFactory.openSession();
	public List<UserPackage> getListUserPackages(){
		session.beginTransaction();
		String queryString="select * from user_package";
		Query query=session.createSQLQuery(queryString).addEntity(UserPackage.class);
		return query.list();
	}
	public int insert(UserPackage userPackage,Package pack,User user){
		userPackage.setOwnPackage(pack);
		userPackage.setOwnUser(user);
		Transaction tx=session.beginTransaction();
		session.save(userPackage);
		tx.commit();
		return 1;
	}
	public List<UserPackage> getListUserPackgeByUserId(int customerId){
		session.beginTransaction();
		String queryString="select * from user_package where fk_user_id=:id";
		Query query=session.createSQLQuery(queryString).addEntity(UserPackage.class);
		query.setParameter("id",customerId);
		return query.list();
	}
	public int deleteByUserId(int id){
		Transaction tx=session.beginTransaction();
		String queryString="delete from user_package where fk_user_id= :id";
		Query query=session.createSQLQuery(queryString).setParameter("id", id);
		int count=query.executeUpdate();
		tx.commit();
		return count;
	}
	public int delete(UserPackage userPackage){
		Transaction tx=session.beginTransaction();
		session.delete(userPackage);
		tx.commit();
		return 1;
	}
}

